<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 头部组件 -->
      <PageTool>
        <template #after>
          <el-button type="primary" @click="addFn('0','1')">添加权限</el-button>
        </template>
      </PageTool>

      <el-table
        :data="tableData"
        style="width: 100%"
        row-key="id"
        border
      >
        <el-table-column prop="title" label="权限名称" />
        <el-table-column prop="code" label="权限标识" />
        <el-table-column prop="description" label="权限描述" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.pid === '0'" size="small" type="primary" @click="addFn(row.id,'2')">添加子权限</el-button>
            <el-button size="small" @click="editFn(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 弹窗 -->
      <addPermission ref="addPerm" :show-dialog.sync="visibleDialog" @updateEvent="getPermissionList" />
    </div>
  </div>
</template>

<script>
import { getPermission, delPermission, getAgentsCount } from '@/api/permission'
import addPermission from './components/addPermission.vue'
export default {
  components: {
    addPermission
  },
  data() {
    return {
      tableData: [],
      visibleDialog: false,
      limit: 100, // 当页条数
      start: 0, // 来时获取的位置
      total: 1 // 总数
    }
  },
  created() {
    // 获取权限列表
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      const res = await getPermission({
        _limit: this.limit,
        _start: this.start
      })
      // console.log(res)
      // this.tableData = res.data
      this.tableData = this.listToTree(res.data, '0')
      // console.log(this.tableData)
      // 获取权限总数量
      const result = await getAgentsCount()
      this.total = result.data
    },
    // 添加权限
    // addFn() {
    //   this.$refs.addPerm.formDate.pid = '0'
    //   this.$refs.addPerm.formDate.type = '1'
    //   this.visibleDialog = true
    // },
    // // 添加子权限
    // addFn1(id) {
    //   this.$refs.addPerm.formDate.pid = `${id}`
    //   this.$refs.addPerm.formDate.type = '2'
    //   this.visibleDialog = true
    // },
    addFn(pid, type) {
      this.$refs.addPerm.formDate.pid = '' + pid
      this.$refs.addPerm.formDate.type = type
      this.visibleDialog = true
    },
    // 删除权限
    delFn(id) {
      this.$confirm('确定要删除此权限吗', '删除', {
        type: 'warning'
      }).then(async() => {
        // 删除成功-调用接口
        await delPermission(id)
        // 刷新列表
        this.getPermissionList()
      }).catch(() => {
        console.log('取消删除')
      })
    },
    // 点击编辑按钮
    async editFn(id) {
      // 显示弹窗
      this.visibleDialog = true
      // 获取子组件实例，调用子组件方法-传id
      this.$refs.addPerm.editPermission(id)
    },
    // 树形
    listToTree(arr, id) {
      const list = []
      arr.forEach(item => {
        // item.hasChildren = false
        // if (Number(item.pid) === Number(id)) {
        //   item.hasChildren = true
        //   list.push(item)
        //   item.children = this.listToTree(arr, item.id)
        // }
        if (item.pid === '' + id) {
          item.children = this.listToTree(arr, item.id)
          list.push(item)
        }
      })
      return list
    }
  }

}
</script>

<style>

</style>
